package com.crayon.account.fragmentspop;

import android.os.Bundle;
import android.view.View.OnClickListener;

import com.crayon.account.dao.DAOCardBank;
import com.crayon.account.dao.DataBaseManager;
import com.crayon.account.data.ItemList;

import java.io.Serializable;

public class FragmentPopEditBank extends FragmentPopEditRoot implements OnClickListener{

	DAOCardBank daoCardBank;
	public static FragmentPopEditBank newInstance(ItemList item) {
		FragmentPopEditBank f = new FragmentPopEditBank();
		// Supply num input as an argument.
		Bundle bundle = new Bundle();
		if(item!=null){
			bundle.putSerializable("item", item);
			bundle.putSerializable("dao", (Serializable)item.getTag());
		}
		f.setArguments(bundle);
		return f;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if(getArguments()!=null){
			daoCardBank = (DAOCardBank)getArguments().getSerializable("dao");
		}
	}

	@Override
	public void OnEventNewCreat() {
		// TODO Auto-generated method stub
		daoCardBank.setId(0);
		daoCardBank.setUniqueName(null);
		daoCardBank.setSummary(null);
	}

	@Override
	public void OnEventDelete() {
		DataBaseManager baseManager = DataBaseManager.getHelper(getActivity());
		try {
			baseManager.getDAOCardBank().delete(daoCardBank);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	@Override
	public boolean OnEventUpdate(String cate) {
		DataBaseManager baseManager = DataBaseManager.getHelper(getActivity());
		try {
			if(daoCardBank!=null){
				daoCardBank.setUniqueName(cate);
				daoCardBank.setSummary(cate);
				if(daoCardBank.getId()>0){
					baseManager.getDAOCardBank().update(daoCardBank);

				}else{
					baseManager.getDAOCardBank().create(daoCardBank);
				}
				return true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		}
		return false;
	}
}